package itcast.bos.base.service.imp;

import java.util.List;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import itcast.bos.base.dao.CourierRepostory;
import itcast.bos.base.service.ICourierService;
import itcast.bos.domain.base.Courier;
import itcast.bos.domain.base.TakeTime;

@Service
@Transactional
public class CourierService implements ICourierService {
	
	@Autowired
	private CourierRepostory cr;


	@Override
	public void save(Courier courier) {
		cr.save(courier);
		
	}



	@Override
	public Page<Courier> findPageData(Specification<Courier> sf, Pageable pageable) {
		return cr.findAll(sf,pageable);
		//return null;
	}



	@Override
	public void delete(String[] sts) {
		for (String s : sts) {
			int i = Integer.parseInt(s);
			cr.update(i);
		}
		
	}


	//查找没有关联定去的快递员
	@Override
	public List<Courier> findNoFixedAreaCouriers() {
		Specification<Courier> sf=new Specification<Courier>() {
			
			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate p = cb.isEmpty(root.get("fixedAreas").as(Set.class));
				return p;
			}
		};
		List<Courier> list=cr.findAll(sf);
		return list;
	}



	@Override
	public Courier findOne(String courierId) {
		int id = Integer.parseInt(courierId);
		return cr.findOne(id);
	}



	@Override
	public void updateCourierByTime(String courierId, TakeTime tt) {
		int id = Integer.parseInt(courierId);
		cr.updateCourierByTime(id,tt);
		
	}




}
